//导入http
const http = require('http');
//导入fs(文件读写对象)
const fs = require('fs');

// 创建server对象
const server = http.createServer((req, res) => {
    // 获取请求的方法
    let {method} = req;
    //获取请求的url路径
    let {pathname} = new URL(req.url, 'http://127.0.0.1');
    // 文件读取的路径
    let path=__dirname+pathname
    console.log(path)
    // 异步文件读取
    fs.readFile(path,(err, data) => {
        if (err){
            res.setHeader('content-type','text/html;charset=utf-8')
            res.statusCode=500;
            res.end('文件读取失败~~');
            // 设置响应状态描述
            res.statusMessage = 'error';
            return 0;
        }
        res.end(data);
    });
});

// 开启服务
server.listen(9000, () => {
    console.log('The service has already been opened!!!');
});